package inl.resilienceManager.qmul.gemom;

import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

import resilienceManager.qmul.gemom.Gbroker;

/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */
//FIXME original workload + bidding workload

public class iniRisk extends ApplicationFrame
{
	public iniRisk(String paramString)
	{
		super(paramString);

	}

	private static CategoryDataset createDataset1(solution[] sl,Gbroker[] glist)
	{

		String str1 = "Size";

		DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();

		for(int i=0; i<glist.length; i++)
		{
			String name=sl[i].toString();
			double s= sl[i].Size();

			localDefaultCategoryDataset.addValue(new Double(s), str1, name);

		}
		return localDefaultCategoryDataset;

	}

	private static CategoryDataset createDataset2(solution[] sl,Gbroker[] glist)
	{
		String str1 = "Risk Value (%)";


		DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
		
		
		double whole_risk=0;

		for(int i=0; i<glist.length; i++)
		{
			//String name="Gbroker-"+ glist[i].getID();

			String name=sl[i].toString();
			double r= glist[i].getRisk(sl[i]);
			localDefaultCategoryDataset.addValue(new Double(r), str1, name);
			if(i==0)
				whole_risk=1-r;
			else
				whole_risk=whole_risk*(1-r);
		}
		whole_risk=1-whole_risk;
		System.out.println("The system risk="+whole_risk);


		return localDefaultCategoryDataset;
	}

	private static JFreeChart createChart(solution[] sl,Gbroker[] glist)
	{
		JFreeChart localJFreeChart = ChartFactory.createBarChart("Initialization Allocation", "Item Combination", "Item size", 
				createDataset1(sl, glist), PlotOrientation.VERTICAL, false, true, false);
		localJFreeChart.setBackgroundPaint(Color.white);
		CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
		localCategoryPlot.setBackgroundPaint(new Color(238, 238, 255));
		localCategoryPlot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		CategoryDataset localCategoryDataset = createDataset2(sl,glist);
		localCategoryPlot.setDataset(1, localCategoryDataset);
		localCategoryPlot.mapDatasetToRangeAxis(1, 1);
		CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
		localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		NumberAxis localNumberAxis = new NumberAxis("Risk Value");
		localCategoryPlot.setRangeAxis(1, localNumberAxis);
		LineAndShapeRenderer localLineAndShapeRenderer = new LineAndShapeRenderer();
		localLineAndShapeRenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
		localCategoryPlot.setRenderer(1, localLineAndShapeRenderer);
		localCategoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
		LegendTitle localLegendTitle1 = new LegendTitle(localCategoryPlot.getRenderer(0));
		localLegendTitle1.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
		localLegendTitle1.setBorder(new BlockBorder());
		LegendTitle localLegendTitle2 = new LegendTitle(localCategoryPlot.getRenderer(1));
		localLegendTitle2.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
		localLegendTitle2.setBorder(new BlockBorder());
		BlockContainer localBlockContainer = new BlockContainer(new BorderArrangement());
		localBlockContainer.add(localLegendTitle1, RectangleEdge.LEFT);
		localBlockContainer.add(localLegendTitle2, RectangleEdge.RIGHT);
		localBlockContainer.add(new EmptyBlock(2000.0D, 0.0D));
		CompositeTitle localCompositeTitle = new CompositeTitle(localBlockContainer);
		localCompositeTitle.setPosition(RectangleEdge.BOTTOM);
		localJFreeChart.addSubtitle(localCompositeTitle);
		return localJFreeChart;
	}

	public static JPanel createPanel(solution[] sl,Gbroker[] glist)
	{
		JFreeChart localJFreeChart = createChart(sl, glist);
		return new ChartPanel(localJFreeChart);
	}

}
